package com.xtaller.easy.admin.service.impl;

import com.xtaller.easy.admin.base.TServiceImpl;
import com.xtaller.easy.admin.mapper.SysModuleMapper;
import com.xtaller.easy.admin.model.SysMenu;
import com.xtaller.easy.admin.model.SysModule;
import com.xtaller.easy.admin.service.ISysModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import com.xtaller.common.convert.W;
import com.xtaller.common.bean.Where;
/**
* Created by Taller on 2018/04/23
*/
@Service
public class SysModuleService extends TServiceImpl<SysModuleMapper,SysModule> implements ISysModuleService {
   @Autowired
   private SysMenuService menuService;
    /**************************CURD begin******************************/
   // 创建
   @Override
   public SysModule createSysModule(SysModule model) {
       if(this.insert(model))
           return this.selectById(model.getId());
       return null;
   }
 
   // 删除
   @Override
   public Boolean deleteSysModule(Object ids,String reviser) {
       return this.delete(ids,reviser);
   }
 
   // 修改
   @Override
   public SysModule updateSysModule(SysModule model) {
       if(this.update(model))
           return this.selectById(model.getId());
       return null;
   }
 
   // 查询
   @Override
   public List<SysModule> findByIds(Object ids) {
       return this.selectByIds(ids);
   }
 
   // 属于
   @Override
   public Boolean exist(List<Where> w) {
       w.add(new Where("1"));
       return this.query(w).size() > 0;
   }
 
   // 查询一个id是否存在
   @Override
   public Boolean existId(Object id) {
       where = W.f(
               W.and("id","eq",id),
               W.field("1")
       );
       return this.query(where).size() > 0;
   }



    /**************************CURD end********************************/
    @Override
    public Boolean existCode(SysModule model, boolean isCreate) {
        if(!isCreate){
            where = W.f(
                    W.and("code", "eq", model.getCode()),
                    W.and("id", "ne", model.getId())
            );
        }else{
            where = W.f(
                    W.and("code", "eq", model.getCode())
            );
        }
        return this.queryOne(where) == null ? false : true;
    }

    @Override
    public Boolean existName(SysModule model, boolean isCreate) {
        if(!isCreate){
            where = W.f(
                    W.and("name", "eq", model.getName()),
                    W.and("id", "ne", model.getId())
            );
        }else{
            where = W.f(
                    W.and("name", "eq", model.getName())
            );
        }
        return this.queryOne(where) == null ? false : true;
    }

    @Override
    public Boolean existMenu(String id) {
        List<SysMenu> menus = menuService.query(W.f(
                W.and("moduleId", "eq", id)
        ));

        return menus.size() > 0 ? true : false;
    }
}